{ "cells": [ { "cell_type": "markdown", "id": "1532aed2", "metadata": {}, "source": [ "## String" ] }, { "cell_type": "markdown", "id": "0e05a2e4", "metadata": {}, "source": [ "- Immutable" ] }, { "cell_type": "markdown", "id": "4147a9c8", "metadata": {}, "source": [ "### List to string" ] }, { "cell_type": "markdown", "id": "865a393e", "metadata": {}, "source": [ "- 'separator'.join(iterable) \n", " - takes list as input\n", " - returns string which is concat of these list elements" ] }, { "cell_type": "code", "execution_count": 3, "id": "24444ac5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'SahilChoudhary'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "''.join(['Sahil' 'Choudhary'])" ] }, { "cell_type": "code", "execution_count": 4, "id": "0d79b1e5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'A,B,C,D'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "','.join(['A','B','C','D'])" ] }, { "cell_type": "markdown", "id": "92f01d8a", "metadata": {}, "source": [ "### String to List" ] }, { "cell_type": "markdown", "id": "6472d1c2", "metadata": {}, "source": [ "- str_var.split('-')\n", " - Takes String\n", " - Returns List" ] }, { "cell_type": "code", "execution_count": 5, "id": "bc324f7d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['sahil', 'choudhary']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'sahil-choudhary'.split('-')" ] }, { "cell_type": "markdown", "id": "e489eafa", "metadata": {}, "source": [ "### Iterating over Strings" ] }, { "cell_type": "code", "execution_count": 14, "id": "43b591bb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['s', 'a', 'h', 'i', 'l', ' ', 'c', 'h', 'o', 'u', 'd', 'h', 'a', 'r', 'y']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# For iterations,for loop is not pythonic way,using comprehensions is pythonic way\n", "\n", "s='sahil choudhary'\n", "[x for x in s]" ] }, { "cell_type": "markdown", "id": "4c55c8af", "metadata": {}, "source": [ "### Reversing a String" ] }, { "cell_type": "code", "execution_count": 11, "id": "b5990c57", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'lihas'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'sahil'[::-1]" ] }, { "cell_type": "markdown", "id": "8bcdb23b", "metadata": {}, "source": [ "### Note\n", "- input() always takes input as string\n", " - convert it to int/float as required" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }